package com.ict.system.intercept;


import com.ict.system.config.FileUploadConfig;
import com.ict.system.dto.BaseResponse;
import com.ict.system.exceptions.FileUploadException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import lombok.extern.slf4j.Slf4j;

@ControllerAdvice
@Slf4j
public class GlobalExceptionResolver {

	@Autowired
	private FileUploadConfig fileUploadConfig;

	/**
	 * 文件超出大小了返回错误信息
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(MaxUploadSizeExceededException.class)
	@ResponseBody
	public BaseResponse handleBusinessException(MaxUploadSizeExceededException ex) {
		return BaseResponse.createFail("图片超过"+ fileUploadConfig.getImageLimit() / 1024 / 1024+ "M");
	}

	/**
	 * 处理所有业务异常
	 *
	 * @param e 业务异常
	 * @return json结果
	 */
	@ExceptionHandler(FileUploadException.class)
	@ResponseBody
	public BaseResponse handleFileUploadException(FileUploadException e) {
		log.warn("文件上传发生异常", e);
		return BaseResponse.createFail(e.getCode(), e.getMessage());
	}

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public BaseResponse handleException(Exception e) {
		log.warn("文件上传发生异常", e);
		return BaseResponse.createFail(e.getMessage());
	}

}
